local ViewWorld = Game:addState("ViewWorld")

function ViewWorld:enteredState()
	print("entered ViewWorld state")
	self.air_tile = 0
	self.water_tile = 1
	self.land_tile = 2
	self.camera = Camera:new()
	
	self:create_drawable_map()
	self.draw_map = function()
		local tw = self.world.data.tile_width
		local th = self.world.data.tile_height
		local maxX = self.world.data.stats.long
		local maxY = self.world.data.stats.lat
		local maxZ = self.world.data.stats.ele
		local map = self.world.data.grid
		local tile_opts = {
				[0] = function(x, y, z, maxZ) -- air

				end,
				[1] = function(x, y, z, maxZ) -- water
					local cm = self:get_view_cm(z, maxZ)
					love.graphics.setColor(81*cm, 101*cm, 255*cm)
					love.graphics.rectangle("fill", x*tw, y*th, tw, th)
					love.graphics.setColor(255, 255, 255)
				end,
				[2] = function(x, y, z, maxZ) -- land
					local cm = self:get_view_cm(z, maxZ)
					--love.graphics.setColor(150*cm, 64*cm, 36*cm)
					love.graphics.setColor(20*cm, 135*cm, 35*cm)
					love.graphics.rectangle("fill", x*tw, y*th, tw, th)
					love.graphics.setColor(255, 255, 255)
				end,
			}
		if self.camera.view_mode == "all" then
			for i = 1, #self.drawable_map do
				local tile = self.drawable_map[i]
				tile_opts[tile.type](tile.x, tile.y, tile.z, maxZ)
			end
		elseif self.camera.view_mode == "layer" then
			for y = 1, maxY do
				for x = 1, maxX do
					local tile = map[self.camera.z][y][x]
					if tile then
						tile_opts[tile.type](x, y, self.camera.z, maxZ)
					end
				end
			end
		end

	end
	self.camera:newLayer(1, self.draw_map)
end

function ViewWorld:get_view_cm(z, maxZ)
	local cm
	if self.camera.view_mode == "all" then 
		return z/maxZ
	elseif self.camera.view_mode == "layer" then
		return 1
	end
end

function ViewWorld:update(dt)
	
end


function ViewWorld:keypressed(key, isRepeat)
	if key == "kp+" then
		self.camera.scaleX = self.camera.scaleX/2
		self.camera.scaleY = self.camera.scaleY/2
	end

	if key == "kp-" then
		self.camera.scaleX = self.camera.scaleX*2
		self.camera.scaleY = self.camera.scaleY*2
	end

	if key == "kp8" then
		self.camera.y = self.camera.y-1*self.world.data.tile_height/self.camera.scaleY
	end

	if key == "kp2" then
		self.camera.y = self.camera.y+1*self.world.data.tile_height/self.camera.scaleY
	end

	if key == "kp6" then
		self.camera.x = self.camera.x+1*self.world.data.tile_width/self.camera.scaleX
	end

	if key == "kp4" then
		self.camera.x = self.camera.x-1*self.world.data.tile_width/self.camera.scaleX
	end

	if key == "[" then
		if self.camera.z-1 > 0 then
			self.camera.z = self.camera.z-1
		else
			self.camera.z = self.world.data.stats.ele
		end
		print("z level: " .. self.camera.z)
	end

	if key == "]" then
		if self.camera.z+1 <= self.world.data.stats.ele then
			self.camera.z = self.camera.z+1
		else
			self.camera.z = 1
		end
		print("z level: " .. self.camera.z)
	end

	if key == "v" then
		self.camera:cycle_view()
	end

end

function ViewWorld:keyreleased(key)
end

function ViewWorld:mousepressed(x, y, button)
end

function ViewWorld:mousereleased(x, y, button)
end

function ViewWorld:draw()
	self.camera:draw_layers()
end


function ViewWorld:exitedState()
	print("exited ViewWorld state")
end